package com.singfan.core.dao.database.config;

import com.singfan.core.dao.database.model.config.*;

import java.util.*;

/**
 * 所有config数据库数据缓存管理类
 * Author: hansonghe
 * Date: 2015-08-19 13:54
 * Desc:
 */
public class ConfigDataManager {
    private static ConfigDataManager instance = new ConfigDataManager();
    private ConfigDataManager() {
    }
    public static ConfigDataManager getInstance() {
        return instance;
    }

    private Map<Integer, BarberPositionConfig> barberPositionConfigMap;
    private Map<Integer, BusinessCircleConfig> businessCircleConfigMap;
    private Map<Integer, CityConfig> cityConfigMap;
    private Map<Integer, DistrictConfig> districtConfigMap;
    private Map<Integer, MasterShopConfig> masterShopConfigMap;
    private Map<Integer, ProvinceConfig> provinceConfigMap;

    public BarberPositionConfig getBarberPositionConfig(int configId) {
        return barberPositionConfigMap.get(configId);
    }

    public BusinessCircleConfig getBusinessCircleConfigMap(int configId) {
        return businessCircleConfigMap.get(configId);
    }

    public CityConfig getCityConfigMap(int configId) {
        return cityConfigMap.get(configId);
    }

    public DistrictConfig getDistrictConfigMap(int configId) {
        return districtConfigMap.get(configId);
    }

    public MasterShopConfig getMasterShopConfigMap(int configId) {
        return masterShopConfigMap.get(configId);
    }

    public ProvinceConfig getProvinceConfigMap(int configId) {
        return provinceConfigMap.get(configId);
    }

    public List<MasterShopConfig> getMasterShopConfigList(){
        List<MasterShopConfig> result = new ArrayList<>();
        if (masterShopConfigMap != null && !masterShopConfigMap.isEmpty()){
            Collection<MasterShopConfig> configCollection = masterShopConfigMap.values();
            for (MasterShopConfig masterShopConfig : configCollection){
                result.add(masterShopConfig);
            }
        }
        return result;
    }

    public void loadConfigData(){
        System.out.println("ConfigDataManager:loadConfigData() begin ... ");
        loadProvinceConfig();
        loadCityConfig();
        loadDistrictConfig();
        loadBusinessCircleConfig();
        loadMasterShopConfig();
        loadBarberPositionConfig();
        System.out.println("ConfigDataManager:loadConfigData() successful ! ");
    }

    public void loadProvinceConfig(){
        List<ProvinceConfig> provinceConfigList = SingFanConfigDao.getInstance().selectAll(ProvinceConfig.class);
        Map<Integer, ProvinceConfig> map = new HashMap<>();
        for (ProvinceConfig provinceConfig : provinceConfigList){
            map.put(provinceConfig.getId(), provinceConfig);
        }
        provinceConfigMap = map;

        System.out.println("ConfigDataManager:loadProvinceConfig() successful !");
    }

    public void loadCityConfig(){
        List<CityConfig> cityConfigList = SingFanConfigDao.getInstance().selectAll(CityConfig.class);
        Map<Integer, CityConfig> map = new HashMap<>();
        for (CityConfig cityConfig : cityConfigList){
            map.put(cityConfig.getId(), cityConfig);
        }
        cityConfigMap = map;

        System.out.println("ConfigDataManager:loadCityConfig() successful !");
    }

    public void loadDistrictConfig(){
        List<DistrictConfig> districtConfigList = SingFanConfigDao.getInstance().selectAll(DistrictConfig.class);
        Map<Integer, DistrictConfig> map = new HashMap<>();
        for (DistrictConfig districtConfig : districtConfigList){
            map.put(districtConfig.getId(), districtConfig);
        }
        districtConfigMap = map;

        System.out.println("ConfigDataManager:loadDistrictConfig() successful !");
    }

    public void loadBusinessCircleConfig(){
        List<BusinessCircleConfig> businessCircleConfigList = SingFanConfigDao.getInstance().selectAll(BusinessCircleConfig.class);
        Map<Integer, BusinessCircleConfig> map = new HashMap<>();
        for (BusinessCircleConfig businessCircleConfig : businessCircleConfigList){
            map.put(businessCircleConfig.getId(), businessCircleConfig);
        }
        businessCircleConfigMap = map;

        System.out.println("ConfigDataManager:loadBusinessCircleConfig() successful !");
    }

    public void loadBarberPositionConfig(){
        List<BarberPositionConfig> barberPositionList = SingFanConfigDao.getInstance().selectAll(BarberPositionConfig.class);
        Map<Integer, BarberPositionConfig> map = new HashMap<>();
        for (BarberPositionConfig barberPositionConfig : barberPositionList){
            map.put(barberPositionConfig.getId(), barberPositionConfig);
        }
        barberPositionConfigMap = map;

        System.out.println("ConfigDataManager:loadBarberPositionConfig() successful !");
    }

    public void loadMasterShopConfig(){
        List<MasterShopConfig> masterShopConfigList = SingFanConfigDao.getInstance().selectAll(MasterShopConfig.class);
        Map<Integer, MasterShopConfig> map = new HashMap<>();
        for (MasterShopConfig masterShopConfig : masterShopConfigList){
            map.put(masterShopConfig.getId(), masterShopConfig);
        }
        masterShopConfigMap = map;

        System.out.println("ConfigDataManager:loadMasterShopConfig() successful !");
    }
}
